/**
 * 个人中心
 */
bangkeapp.factory('centerService', ['$http', '$q', 'url', 'userinfoService', '$ionicLoading',
	function($http, $q, url, userinfoService, $ionicLoading) {
		var myinfo = function(member) {
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});
			var data = userinfoService.getData();
			var member = userinfoService.getUser();
			data.request.body.member = []
			data.request.body.member[0] = member;

			var deferred = $q.defer();
			var promise = deferred.promise;
			var progress;

			$http({
					method: "JSONP",
					url: url + "/member/query?callback=JSON_CALLBACK",
					params: {
						"body": angular.toJson(data)
					}
				}).success(function(response) {

					var result = response;
					progress = "正确";
					deferred.notify(progress);
					deferred.resolve(result);
					console.log(result);
					$ionicLoading.hide();
				})
				.error(function(error) {
					deferred.reject(error);
				});
			return promise;
		};
		var savememberinfo = function(member) {

			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay: 0
			});

			var data = userinfoService.getData();
			data.request.body.member = []
			data.request.body.member[0] = member;

			var deferred = $q.defer();
			var promise = deferred.promise;
			var progress;

			$http({
					method: "JSONP",
					url: url + "/member/save?callback=JSON_CALLBACK",
					params: {
						"body": angular.toJson(data)
					}
				}).success(function(response) {

					var result = response;
					progress = "正确";
					deferred.notify(progress);
					deferred.resolve(result);
					console.log(result);
					$ionicLoading.hide();
				})
				.error(function(error) {
					deferred.reject(error);
				});
			return promise;
		};
		var interests = function() {

			var result = {};

			var data = userinfoService.getData();
			var member = userinfoService.getUser();
			data.request.body.lstInterests = []
			data.request.body.lstInterests[0] = member;

			var deferred = $q.defer();
			var promise = deferred.promise;
			var progress;
			// 查询红包积分
			$http({
				method: "JSONP",
				url: url + "/interests/query?callback=JSON_CALLBACK",
				params: {
					"body": angular.toJson(data)
				}
			}).success(function(response) {

				if("M" == response.Response.Header.ResponseCode &&
					response.Response.Body.Response.length > 0) {

					if(member.memberid == response.Response.Body.Response[0].memberid) {
						result.interests = response.Response.Body.Response[0].interests;
						progress = "正确";
					} else {
						result.interests = 0;
						progress = "错误";
					}
				} else {
					result.interests = 0;
					progress = response.Response.Header.MsgList[0];
				}
				deferred.notify(progress);
				deferred.resolve(result);
			})
			.error(function(error) {
				deferred.reject(error);
				result.interests = 0;
			});
			return promise;
		}
		var equity = function() {
			$ionicLoading.show({
				content: 'Loading',
				animation: 'fade-in',
				showBackdrop: true,
				maxWidth: 200,
				showDelay:0
			});
			
			var data = userinfoService.getData();
			var member = userinfoService.getUser();
			data.request.body.lstInterests = []
			data.request.body.lstInterests[0] = member;
			
			var deferred = $q.defer();
			var promise = deferred.promise;
			var progress;
			
			//初始股权积分
			$http({
				method: "JSONP",
				url: url + "/advancerecharge/saveOrUpdate?callback=JSON_CALLBACK",
				params:{
					"body": angular.toJson(data)
				}
			}).success(function(response){
				var result = response;
				progress = "正确";
				deferred.notify(progress);
				deferred.resolve(result);
			})
			.error(function(error){
				deferred.reject(error);
				result.interests = 0;
			});
			return promise;
			
		}
		return {
			myinfo: myinfo,
			savememberinfo: savememberinfo,
			interests: interests
		};
	}
]);